﻿Imports EDMDesigner.Business.SSDL

Imports <xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">

Friend Module WriterSSDL
    Friend Function WriteSSDL(ByVal ssdl As SSDLContainer) As XElement
        Dim ssdlNS = String.Concat(ssdl.Namespace, ".")
        Dim storeNS As XNamespace = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
        Return <Schema Namespace=<%= ssdl.Namespace %> Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
                   <EntityContainer Name=<%= ssdl.Name %>>
                       <%= From entitySet In ssdl.EntityTypes _
                           Select <EntitySet Name=<%= entitySet.EntitySetName %> EntityType=<%= String.Concat(ssdlNS, entitySet.Name) %>/> _
                           .AddAttribute("Schema", entitySet.Schema) _
                           .AddAttribute(storeNS, "Name", entitySet.StoreName) _
                           .AddAttribute(storeNS, "Schema", entitySet.StoreSchema) _
                           .AddAttribute(storeNS, "Type", entitySet.StoreType) _
                           .AddAttribute("Table", entitySet.Table) _
                           .AddElement(If(entitySet.DefiningQuery Is Nothing, Nothing, <DefiningQuery><%= entitySet.DefiningQuery %></DefiningQuery>)) %>
                       <%= From associationSet In ssdl.AssociationSets _
                           Select <AssociationSet Name=<%= associationSet.AssociationSetName %> Association=<%= String.Concat(ssdlNS, associationSet.Name) %>>
                                      <End Role=<%= associationSet.Role1.Name %> EntitySet=<%= associationSet.Role1.Type.Name %>/>
                                      <End Role=<%= associationSet.Role2.Name %> EntitySet=<%= associationSet.Role2.Type.Name %>/>
                                  </AssociationSet> %>
                   </EntityContainer>
                   <%= From entityType In ssdl.EntityTypes _
                       Select <EntityType Name=<%= entityType.Name %>>
                                  <Key>
                                      <%= From column In entityType.Properties _
                                          Where column.IsKey _
                                          Select <PropertyRef Name=<%= column.Name %>/> %>
                                  </Key>
                                  <%= From column In entityType.Properties _
                                      Select <Property Name=<%= column.Name %> Type=<%= column.Type %>/> _
                                      .AddAttribute("Collation", column.Collation) _
                                      .AddAttribute("DefaultValue", column.DefaultValue) _
                                      .AddAttribute("FixedLength", column.FixedLength) _
                                      .AddAttribute("MaxLength", column.MaxLength) _
                                      .AddAttribute("Nullable", column.Nullable) _
                                      .AddAttribute("Precision", column.Precision) _
                                      .AddAttribute("Scale", column.Scale) _
                                      .AddAttribute("StoreGeneratedPattern", column.StoreGeneratedPattern) _
                                      .AddAttribute(storeNS, "Name", column.StoreName) _
                                      .AddAttribute(storeNS, "Schema", column.StoreSchema) _
                                      .AddAttribute(storeNS, "Type", column.StoreType) _
                                      .AddAttribute("Unicode", column.Unicode) %>
                              </EntityType> %>
                   <%= From association In ssdl.AssociationSets _
                       Select <Association Name=<%= association.Name %>>
                                  <End Role=<%= association.Role1.Name %> Type=<%= String.Concat(ssdlNS, association.Role1.Type.Name) %> Multiplicity=<%= EDMDesigner.Business.AssociationBase.CardinalityStringConverter.CardinalityToSTring(association.Role1.Cardinality) %>/>
                                  <End Role=<%= association.Role2.Name %> Type=<%= String.Concat(ssdlNS, association.Role2.Type.Name) %> Multiplicity=<%= EDMDesigner.Business.AssociationBase.CardinalityStringConverter.CardinalityToSTring(association.Role2.Cardinality) %>/>
                                  <ReferentialConstraint>
                                      <Principal Role=<%= association.PrincipalRole.Name %>>
                                          <%= From prop In association.PrincipalRole.Properties _
                                              Select <PropertyRef Name=<%= prop.Name %>/> %>
                                      </Principal>
                                      <Dependent Role=<%= association.DependantRole.Name %>>
                                          <%= From prop In association.DependantRole.Properties _
                                              Select <PropertyRef Name=<%= prop.Name %>/> %>
                                      </Dependent>
                                  </ReferentialConstraint>
                              </Association> %>
                   <%= From func In ssdl.Functions _
                       Select <Function Name=<%= func.Name %>>
                                  <%= From parameter In func.Parameters _
                                      Select <Parameter Name=<%= parameter.Name %> Type=<%= parameter.Type %> Mode=<%= parameter.Mode %>/> _
                                      .AddAttribute("MaxLength", parameter.MaxLength) _
                                      .AddAttribute("Precision", parameter.Precision) _
                                      .AddAttribute("Scale", parameter.Scale) _
                                      .AddAttribute(storeNS, "Name", parameter.StoreName) _
                                      .AddAttribute(storeNS, "Schema", parameter.StoreSchema) _
                                      .AddAttribute(storeNS, "Type", parameter.StoreType) %>
                              </Function> _
                       .AddAttribute("Aggregate", func.Aggregate) _
                       .AddAttribute("BuiltIn", func.BuiltIn) _
                       .AddAttribute("CommandText", func.CommandText) _
                       .AddAttribute("IsComposable", func.IsComposable) _
                       .AddAttribute("NiladicFunction", func.NiladicFunction) _
                       .AddAttribute("ReturnType", func.ReturnType) _
                       .AddAttribute("StoreFunctionName", func.StoreFunctionName) _
                       .AddAttribute("ParameterTypeSemantics", func.ParameterTypeSemantics) _
                       .AddAttribute("Schema", func.Schema) _
                       .AddAttribute(storeNS, "Name", func.StoreName) _
                       .AddAttribute(storeNS, "Schema", func.StoreSchema) _
                       .AddAttribute(storeNS, "Type", func.StoreType) %>
               </Schema> _
               .AddAttribute("Provider", ssdl.Provider) _
               .AddAttribute("ProviderManifestToken", ssdl.ProviderManifestToken)
    End Function
End Module
